﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using System.Data;
using LGSVR.BLL.Sys;

namespace LGSVR_WEB.program
{
    public partial class messageGroupEdit : DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }

        void PageInit()
        {
       

            Dictionary<string, string> dicSingle;
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Pid != 0)
                {
                    dicSingle = GetSigle("messageGroup", "ID=" + Request.QueryString["ID"]);

                    if (dicSingle != null)
                    {
                        txtmessageGroupID.Text = dicSingle["messageGroupID"];
                        txtgroupName.Text = dicSingle["groupName"];
                        txtgrade.Text = dicSingle["grade"];
                        txtcontent.Text = dicSingle["content"];                     
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (HasKey("messageGroup", "messageGroupID", txtmessageGroupID.Text.Trim(), Pid))
            {
                MsgContent = ShowMessage("已存在该节目组编号，请更换！", MessageType.Error, false, true);
                return;
            }
            Dictionary<string, string> dicParam;

           
                dicParam = new Dictionary<string, string>();

                dicParam.Add("messageGroupID", txtmessageGroupID.Text.Trim());
                dicParam.Add("groupName", txtgroupName.Text.Trim());
                dicParam.Add("grade", txtgrade.Text.Trim());
                dicParam.Add("content", txtcontent.Text.Trim());

                // 编辑
                if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
                {
                    if (Utils.IsNumber(Request.QueryString["ID"]))
                    {
                    Edit("messageGroup", dicParam, "ID=" + Pid);

                        if (Result > 0)
                        {
                            MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                            NoRefresh();
                        }
                        else
                        {
                            if (Result == -100)
                            {
                                MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                            }
                            else
                            {
                                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                            }
                        }
                    }
                }
                else  // 新增
                {
                    Insert("messageGroup", dicParam);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                    }
                }
            }
       

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("messageGroupList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("messageGroupList.aspx", false);
            }
        }
    }
}